﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;

public class Options_XML : MonoBehaviour {

	public static void Options2XML(XmlDocument xmlDoc, XmlElement mainElement)
    {
        XmlElement options = xmlDoc.CreateElement("options");
        foreach (var item in SheetSystemManager.sheetSystemManager.optionsManager.optionGroupList)
        {
            XmlElement option_I = xmlDoc.CreateElement("option_I");
            XmlElement description = xmlDoc.CreateElement("description");
            description.InnerText = item.Value.description;
            XmlElement index = xmlDoc.CreateElement("index");
            index.InnerText = item.Value.number.ToString();
            option_I.AppendChild(index);
            option_I.AppendChild(description);
            foreach (var op_II in ((OptionContent_Option)item.Value).content)
            {
                XmlElement option_II = xmlDoc.CreateElement("option_II");
                XmlElement description_II = xmlDoc.CreateElement("description_II");
                description_II.InnerText = op_II.Value.description;
                XmlElement index_II = xmlDoc.CreateElement("index_II");
                index_II.InnerText = op_II.Value.number.ToString();
                option_II.AppendChild(description_II);
                option_II.AppendChild(index_II);
                option_I.AppendChild(option_II);
            }
            options.AppendChild(option_I);
        }
        mainElement.AppendChild(options);
    }

    public static void XML2Options(string xmlPath,string mainNodeName)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(xmlPath);
        XmlNode options = xmlDoc.SelectSingleNode(mainNodeName).SelectSingleNode("options");
        XmlNodeList options_I = options.SelectNodes("option_I");

        foreach (XmlElement item in options_I)
        {
            OptionContent_Option option_I = new OptionContent_Option();
            option_I.description = item.GetElementsByTagName("description")[0].InnerText;
            option_I.number = int.Parse(item.GetElementsByTagName("index")[0].InnerText);

            SheetSystemManager.sheetSystemManager.optionsManager.AddOneOption_I(option_I);
            foreach (XmlElement op_II in item.SelectNodes("option_II"))
            {
                OptionContent_String option_II = new OptionContent_String();
                option_II.description = op_II.GetElementsByTagName("description_II")[0].InnerText;
                option_II.number = int.Parse(op_II.GetElementsByTagName("index_II")[0].InnerText);
                option_I.AddOneContent(option_II);
                SheetSystemManager.sheetSystemManager.optionsManager.AddOneOption_II(option_II);
            }
        }
    }
}
